package com.yilian.xlmusic.android.view.adapter;

import android.view.View;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder;
import com.yilian.xlmusic.android.databinding.ItemHistoryBinding;
import com.yilian.xlmusic.android.view.bean.HistoryPresenter;
import com.yilian.xlmusic.android.view.bean.SharedMessage;
import com.yilian.xlmusic.android.view.bean.SharedViewModel;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class HistoryAdapter extends BaseQuickAdapter<String, BaseDataBindingHolder<ItemHistoryBinding>> {
    SharedViewModel model;
    private HistoryPresenter historyPresenter = new HistoryPresenter();
    public HistoryAdapter(int layoutResId, @Nullable List<String> data, SharedViewModel sharedViewModel) {
        super(layoutResId, data);
        model = sharedViewModel;
    }

    @Override
    protected void convert(@NotNull BaseDataBindingHolder<ItemHistoryBinding> holder, String s) {
        ItemHistoryBinding itemHistoryBinding = holder.getDataBinding();
        if(null!=itemHistoryBinding){
            itemHistoryBinding.itemHistoryText.setText(s);
            itemHistoryBinding.itemHistoryDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    historyPresenter.HistoryItemClick(getRecyclerView(),s, model);
                }
            });
            setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                    SharedMessage message = new SharedMessage();
                    message.setType(0);
                    message.setValue(s);
                    model.select(message);
                    historyPresenter.goSearch(getContext(),message.getValue(),model,getRecyclerView());
                }
            });
            itemHistoryBinding.executePendingBindings();

        }
    }
}
